package controllers

import (
	"time"

	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
	"weiboxs.com/new_h_glasses/utils"
)

type HomeController struct {
	BaseController
}

func (c *HomeController) Index() {

	//判断是否登录
	c.checkLogin()

	c.Data["activeSidebarUrl"] = "/home/index"

	//近七日时间
	startTime := time.Now().Unix() - 86400*7
	endTime := time.Now().Unix()
	//档案数据
	c.archivesTimeSolt(startTime, endTime)
	//验光数据
	c.optometryTimeSolt(startTime, endTime)
	//销售数据
	d, _, _ := services.RetailsTableCurve(c.curUser, startTime, endTime)
	c.Data["retails"] = d
	c.Data["todayMoneyTotal"] = d[time.Now().Format("01-02")]

	//近30日时间
	startTime = time.Now().Unix() - 86400*30
	endTime = time.Now().Unix()
	//档案
	archivesCount := models.QueryArchivesTimeSoltSeterCount(c.curUser,startTime, endTime)
	c.Data["archivesCount"] = archivesCount
	//验光
	optometryCount := models.QueryOptometryTimeSoltSeterCount(&c.curUser,startTime, endTime)
	c.Data["optometryCount"] = optometryCount

	c.Data["endDate"] = time.Unix(endTime, 0).Format("2006-01-02")

	c.display("", "home/index_footerjs.html")
}

func (c *HomeController) initTimeSolt(startTime, endTime int64) map[string]int {

	var skey string

	initStatistics := make(map[string]int) //先初始一个统计表
	for i := startTime; i <= endTime; i += 86400 {
		skey = time.Unix(i, 0).Format("01-02")
		initStatistics[skey] = 0
	}

	return initStatistics
}

//档案数据
func (c *HomeController) archivesTimeSolt(startTime, endTime int64) {

	var skey string

	archivesData := models.QueryArchivesTimeSoltSeter(c.curUser, startTime, endTime)
	var archivesTotal int64 = 0

	statistics := c.initTimeSolt(startTime, endTime)

	for _, v := range archivesData {

		skey = time.Unix(v.CreatedAt, 0).Format("01-02")
		if _, ok := statistics[skey]; ok {
			statistics[skey] = statistics[skey] + 1
		} else {
			statistics[skey] = 1
		}
		archivesTotal += 1
	}
	c.Data["statistics"] = statistics
	c.Data["archivesTotal"] = archivesTotal

}

//验光数据
func (c *HomeController) optometryTimeSolt(startTime, endTime int64) {

	var skey string

	optometryData := models.QueryOptometryTimeSoltSeter(&c.curUser, startTime, endTime)
	var optometryTotal int64 = 0

	optometryStatistics := c.initTimeSolt(startTime, endTime)

	for _, v := range optometryData {

		skey = time.Unix(v.CreatedAt, 0).Format("01-02")
		if _, ok := optometryStatistics[skey]; ok {
			optometryStatistics[skey] = optometryStatistics[skey] + 1
		} else {
			optometryStatistics[skey] = 1
		}
		optometryTotal += 1
	}

	c.Data["optometryStatistics"] = optometryStatistics
	c.Data["optometryTotal"] = optometryTotal
}

func (c *HomeController) Page404() {
	c.display()
}

func (c *HomeController) Error() {
	c.Data["error"] = c.GetString(":error")
	c.display("home/error.html", "", "shared/layout_dialog.html")
}

func (c *HomeController) ClearCache() {
	if err := utils.DelCacheAll(); err != nil {
		c.jsonResult(enums.JRCodeFailed, "缓存清除失败", "")
	}
	c.jsonResult(enums.JRCodeSucc, "缓存清除成功", "")
}

/*
func (c *HomeController) sendMobile() {

	fileName := "mobile.txt"

	data, err := ioutil.ReadFile(fileName)
	if err != nil {
		fmt.Println("File reading error", err)
		return
	}

	mobileString :=  strings.Split(string(data), "\r\n")
	gochan := make(chan string, 10)

	sms,_ := services.NewSms()

	for _,v := range mobileString {
		gochan <- v
		go func(mobile chan string) {
			value, _ := <-mobile
			argv := services.SmsArgv{
				Typeid: 2,
				DepartmentId : 4,
				CreatedBy :"王磊",
				Mobile : value,
				Content : "温馨提示：研究生新生入学体检已经结束，请各位参检同学及时登录研究生信息平台-新生报到-体检预约查询系统查看个人体检结果报告。为保障个人健康，如有异常情况建议选择就近的正规医疗机构及时进行就诊复查，以免耽误病情。",
			}
			if err := sms.Send(&argv); err != nil {
				fmt.Println(err.Error())
			} else {
				fmt.Print(value, "->ok")
			}
		}(gochan)
	}

}
*/
